import java.util.*;
import org.json.*;

public class Main
{
	public static Map<String,Object> userdata = new HashMap<String,Object>();
	private static String SyncKey = "";
	public static String Friends = "";
	
	public static void main(String[] args)
	{
		
		new Thread(){
			@Override
			public void run(){
				
				String uuid = vx.getUUID();
				if(!uuid.equals("")){
					//获取图片
					String imageurl = vx.getQRcode(uuid);
					//临时方法测试
					netutils._GET("https://zxkxz.cn/test/wxtest.php?url=" + imageurl);
					//获取二维码状态
					System.out.println("请扫码登录");
					while(true){
						//start1(uuid);
						
						final Map<String,Object> qrcode = vx.getQRst(uuid);
						int code = (int) qrcode.get("code");
						if(code == 200){
							
							new Thread(){
								@Override
								public void run(){
									l(qrcode.get("uri").toString());
								}
							}.start();
							
							return;
						}else if(code == 201){
							System.out.println("二维码已扫描，请及时确认登录");
						}else if(code == 400){
							main(new String[]{});
							return;
						}
						
					}
					//System.out.println("");
					
				}
			}
		}.start();
		
	}
	
	//
	private static void l(String uri){
		System.out.println("登录完成");
		//获取登录数据
		Map<String,Object> data = vx.loginGetData(uri);
		int code = (int) data.get("code");
		if(code == 0){
			userdata.put("skey",data.get("skey").toString());
			userdata.put("wxsid",data.get("wxsid").toString());
			userdata.put("wxuin",data.get("wxuin").toString());
			userdata.put("pass_ticket",data.get("pass_ticket").toString());
			
			//初始化
			String s = vx.login(userdata.get("skey").toString(),userdata.get("wxuin").toString(),userdata.get("wxsid").toString(),userdata.get("pass_ticket").toString());
			try{
				JSONObject json = new JSONObject(s);
				if(json.getJSONObject("BaseResponse").getInt("Ret") == 0){
					userdata.put("username",json.getJSONObject("User").getString("NickName"));
					System.out.println("你好：" + json.getJSONObject("User").getString("NickName"));
					
					json = json.getJSONObject("SyncKey");
					for(int i = 0;i < json.getJSONArray("List").length();i++){
						String k = json.getJSONArray("List").getJSONObject(i).getInt("Key") + "_" + json.getJSONArray("List").getJSONObject(i).getLong("Val");
						if(i == json.getJSONArray("List").length()-1){
							SyncKey += k;
						}else{
							SyncKey += k + "|";
						}
					}
					
					//开启微信状态通知
					boolean onstatus = vx.onstatus(userdata.get("skey").toString(),userdata.get("wxuin").toString(),userdata.get("wxsid").toString(),userdata.get("pass_ticket").toString(),userdata.get("username").toString());
					if(onstatus){
						System.out.println("已开启微信状态通知");
						//获取联系人列表
						Friends = vx.getContact(userdata.get("skey").toString(),userdata.get("pass_ticket").toString());
						if(Friends.equals("")){
							System.out.println("获取联系人列表失败");
							return;
						}
						//开启心跳包循环
						p();
						
					}else{
						System.out.println("开启微信状态通知失败");
						return;
					}
				}else{
					System.out.println(s);
					System.out.println("初始化失败");
				}
			}catch (JSONException e){
				System.out.println("初始化失败:"+e.toString());
			}


		}else{
			System.out.println("错误信息:"+data.get("message"));
		}
		
	}
	
	
	/*心跳包循环*/
	private static void p(){
		//开启心跳
		while(true){
			boolean push = vx.push(userdata.get("skey").toString(),userdata.get("wxuin").toString(),userdata.get("wxsid").toString(),SyncKey);
			if(push){
				//获取新消息
				String datas = vx.getmsg(userdata.get("skey").toString(),userdata.get("wxuin").toString(),userdata.get("wxsid").toString(),userdata.get("pass_ticket").toString(),SyncKey);
				if(datas.equals("")){
					System.out.println("获取新消息出错");
					return;
				}
				try{
					JSONObject json = new JSONObject(datas);
					//更新synckey
					String synckeys = "";
					JSONArray jsons = json.getJSONObject("SyncKey").getJSONArray("List");
					for(int i = 0;i < jsons.length();i++){
						json = jsons.getJSONObject(i);
						String k = json.getInt("Key") + "_" + json.getLong("Val");
						if(i == jsons.length()-1){
							synckeys += k;
						}else{
							synckeys += k + "|";
						}
					}
					SyncKey = synckeys;

					//提取消息数据
					String fromusername = "";
					String tousername = "";
					String content = "";
					json = new JSONObject(datas);
					jsons = json.getJSONArray("AddMsgList");
					for(int i = 0;i < jsons.length();i++){
						json = jsons.getJSONObject(i);
						fromusername = json.getString("FromUserName");
						tousername = json.getString("ToUserName");
						content = json.getString("Content");
						if(!content.equals("") && !fromusername.equals(tousername)){
							if(fromusername.equals(userdata.get("username").toString())){
								//发出消息
								message.sendmsg(tousername,content);
							}else{
								//收到消息
								message.revicemsg(fromusername,content);
							}
						}
					}
				}catch (JSONException e){
					System.out.println("新消息数据错误:"+e.toString());
				}
			}else{
				System.out.println("心跳数据错误");
				return;
			}
		}
	}
}
